<template>
  <ant-modal
    modalWidth="800"
    modalHeight="600"
    :visible="open"
    modal-title="设置生产工序"
    :adjust-size="true"
    @cancel="cancel"
  >
    <a-row slot="content">
    <advance-table
      :pagination="{
          current: queryParam.pageNum,
          pageSize: queryParam.pageSize,
          total: total,
          showSizeChanger: true,
          showLessItems: true,
          showQuickJumper: true,
          showTotal: (total, range) => `第 ${range[0]}-${range[1]} 条，总计 ${total} 条`,
          onChange: changeSize,
          onShowSizeChange: onShowSizeChange
        }"
      :customRow="customRow"
      tableKey="base-bizProductionProcess-index-table"
      @change="handleTableChange"
      rowKey="id"
      size="middle"
      @refresh="getList"
      :columns="columns"
      :data-source="bizProductionProcessList"
      :loading="loading"
      :format-conditions="true"
      :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: 'radio' }"
    >
    </advance-table>
    </a-row>
    <template slot="footer">
      <a-button :disabled="disabled" @click="cancel">
        取消
      </a-button>
      <a-button type="primary" :disabled="disabled" @click="confirm(true)">
        确定
      </a-button>
    </template>
  </ant-modal>
</template>
<script>
import {
  listBizProcessPlan
} from '@/api/biz/bizProcessPlan'
import {
  addBizProductProcessPlan,getBizProductProcessPlanByProductId
} from '@/api/biz/bizProductProcessPlan'
import AdvanceTable from '@/components/pt/table/AdvanceTable'
import AntModal from '@/components/pt/dialog/AntModal'
import { addBizProduct, updateBizProduct } from '@/api/biz/bizProduct'
export default {
  name: 'BizProductProcess',
  components: {
    AdvanceTable,
    AntModal
  },
  data () {
    return {
      productId:undefined,
      showAddModal: false,
      showEditModal: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的主键集合
      selectedRowKeys: [],
      // 选中的数据集合
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // label的百分比
      labelCol: { span: 6 },
      // 内容区域的百分比
      wrapperCol: { span: 18 },
      // 生产工序表格数据
      bizProductionProcessList: [],
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        processName: undefined,

        processCode: undefined

      },
      columns: [
        {
          title: '计划编号',
          dataIndex: 'processNumber',
          ellipsis: true,
          align: 'left',
          width: '45%'
        },
        {
          title: '计划名称',
          dataIndex: 'planName',
          ellipsis: true,
          align: 'left',
          width: '45%'
        }
      ],
      open: false,
      closeDialog: true,
      spinning: false,
      delayTime: 100,
      disabled: false,
      id: undefined
    }
  },
  created () {
  },
  methods: {
    /** 查询生产工序列表 */
    getList () {
      this.loading = true
      listBizProcessPlan(this.queryParam).then(response => {
        this.bizProductionProcessList = response.data.list
        this.total = response.data.total
        this.loading = false
      })
    },
    /** 翻页操作 */
    onShowSizeChange (current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSizeChange (current, size) {
      this.queryParam.pageNum = 1
      this.queryParam.pageSize = size
      this.getList()
    },
    /** 翻页操作 */
    changeSize (current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.ids = this.selectedRows.map(item => item.id)
      this.single = selectedRowKeys.length !== 1
      this.multiple = !selectedRowKeys.length
    },
    handleTableChange (pagination, filters, sorter) {
      if (sorter.field !== undefined && sorter.field !== null && sorter.field !== '') {
        this.queryParam.orderByColumn = 'a.' + sorter.field
        this.queryParam.isAsc = sorter.order
      }
      this.getList()
    },
    /** 修改按钮操作 */
    handleSetProcess (row) {
      this.selectedRowKeys=[];
      this.open = true;
      this.productId=row.id;
      this.getList();
      //查询当前工序
      getBizProductProcessPlanByProductId(this.productId).then(res=>{
        let data =res.data;
        data.forEach(item=>{
          this.selectedRowKeys.push(item.processPlanId);
        })
      })
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.$emit('close')
    },
    /** 提交按钮 */
    confirm: function (closeDialog) {
     // this.closeDialog = closeDialog
      this.disabled = true
      // if(this.selectedRowKeys.length==0) {
      //   this.$message.warning('请选择生产工序');
      //   this.disabled = false
      //   return false
      // } else {
        //保存生产工序
        let param = {productId: this.productId};
        if(this.selectedRowKeys.length) {
          param.processPlanId = this.selectedRowKeys[0]
        }
        addBizProductProcessPlan(param).then(response=>{
          this.$message.success('设置成功', 3)
          this.open = false
          this.$emit('ok')
          this.$emit('close')
          this.disabled = false
        })
      // }
    },
    
    customRow (record) {
        return {
          on: {
            click: () => {
              if(this.selectedRowKeys.includes(record.id)){
                this.selectedRowKeys = []
                this.selectedRows = []
              }else{
                this.selectedRowKeys = [record.id]
                this.selectedRows = [record]
              }
            }
          }
        }
      }
  }
}
</script>
